package com.chris.libs.devtools.x.v1.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.material.bottomnavigation.BottomNavigationItemView;
import com.google.android.material.bottomnavigation.BottomNavigationMenuView;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.HashMap;
import java.util.Map;

/**
 * Create by Chris Chan
 * Create on 2023/5/9 18:56
 * Use for:
 * Explain:
 */
public class TabBadgeUtil {
    private static Map<String, View> viewMap = new HashMap<>(4);

    @SuppressLint("RestrictedApi")
    public static void setTabBadge(String tag, Context context, BottomNavigationView navView, int layoutId, int textViewId, int position, int num) {
        if (num <= 0) {
            return;
        }
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) navView.getChildAt(0);
        if (null == menuView) {
            return;
        }
        if (position > menuView.getChildCount() - 1) {
            return;
        }
        BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(position);
        View badge = LayoutInflater.from(context).inflate(layoutId, itemView, false);
        TextView numView = badge.findViewById(textViewId);
        UnReadMsgUtil.show(numView, num);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        lp.leftMargin = menuView.getItemIconSize();
        lp.bottomMargin = MeasureUtil.dp2px(28);
        itemView.addView(badge, lp);
        //放入控件容器便于后续操作
        viewMap.put(tag, badge);
    }

    //隐藏的
    @Deprecated
    private static void hideTabBadge(BottomNavigationView navView, int layoutId, int position) {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) navView.getChildAt(0);
        BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(position);
        //找到角标控件
        int childCount = itemView.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view = itemView.getChildAt(i);
            if (view.getId() == layoutId) {
                view.setVisibility(View.INVISIBLE);
            }
        }
    }

    /**
     * 隐藏
     *
     * @param tag
     */
    public static void hideTabBadge(String tag) {
        View badge = viewMap.get(tag);
        if (null != badge) {
            badge.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 重新刷新
     *
     * @param tag
     * @param num
     */
    public static void refresh(String tag, int textViewId, int num) {
        View badge = viewMap.get(tag);
        if (null != badge) {
            badge.setVisibility(View.VISIBLE);
            TextView numView = badge.findViewById(textViewId);
            UnReadMsgUtil.show(numView, num);
        }
    }
}
